package org.zdc.pivot.wtk;
import haxe.Json;
import org.zdc.core.IllegalArgumentException;

class Point extends nme.geom.Point
{
	public static inline var X_KEY = "x";
	public static inline var Y_KEY = "y";
	
	public function new(x:Float = 0, y:Float = 0) 
	{
		super(x, y);
		this.x = x;
		this.y = y;
	}
	
	public function translate(dx:Float, dy:Float):Point {
		return new Point(dx + x, dy + y);
	}
	
	public function make(object:Dynamic):Point {
		var x = 0;
		var y = 0;
		
		if (object != null) {
			if (Reflect.hasField(object, X_KEY)) {
				x = Reflect.getProperty(object, X_KEY);
			}
			if (Reflect.hasField(object, Y_KEY)) {
				y = Reflect.getProperty(object, Y_KEY);
			}
		}
		
		return new Point(x, y);
	}
	
	public function decode(value:String):Point {
		if (value == null) {
			throw new IllegalArgumentException();
		}
		
		return make(Json.parse(value));
	}
}